var express = require('express');
var router = express.Router();
const axios = require("axios");

/* GET http://localhost:8080/api/token */
router.get('/token', function(req, res, next) {
    let grant_type="client_credential"
    let appid="wx161d42f21bde379a"
    let secret ="35c576d99644f8a0d7568fea290178c3"
    let url ="https://api.weixin.qq.com/cgi-bin/token?grant_type="+grant_type+"&appid="+appid+"&secret="+secret
    axios.get(url)
        .then(result => {
            // handle success
            console.log(result.data);
            global.token=result.data.access_token
            res.json(result.data)
        })
        .catch(error => {
            // handle error
            console.log(error);
        });
});

/* POST http://localhost:8080/api/databasemigrateimport */
router.post('/databasemigrateimport',function(req,res){
    console.log(global.token)
    let token=global.token
    axios.post('https://api.weixin.qq.com/tcb/databasemigrateimport?access_token='+token, {
        env: 'test-0pmu0',
        collection_name:'device',
        file_path:'files/device124.json',
        file_type:1,
        stop_on_error:true,
        conflict_mode:1
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/databasequery */
router.post('/databasequery',function(req,res){
    console.log(global.token)
    let token=global.token
    axios.post('https://api.weixin.qq.com/tcb/databasequery?access_token='+token, {
        env: 'test-0pmu0',
        query:'db.collection(\\"device\\").where({price:1000}).get()'
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/databaseadd */
router.post('/databaseadd',function(req,res){
    console.log(global.token)
    let token=global.token
    axios.post('https://api.weixin.qq.com/tcb/databaseadd?access_token='+token, {
        env: 'test-0pmu0',
        query: 'db.collection(\\"contact\\").add({ ' +
            'data: ' +
            '[' +
            '{name:\\"蹇向秋\\",sex:\\"男\\",mobile: \\"15888837968\\"}, ' +
            '{name:\\"犹淑慧\\",sex:\\"女\\",mobile:\\"13506897988\\"}' +
            ']})'
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})


/* POST http://localhost:8080/api/databaseupdate */
router.post('/databaseupdate',function(req,res){
    console.log(global.token)
    let token = global.token
    axios.post('https://api.weixin.qq.com/tcb/databaseupdate?access_token='+token, {
        env: 'test-0pmu0',
        query: 'db.collection(\\"contact\\").where({name:\\"蹇向秋\\"}).update({data:{sex:\\"女\\"}})'
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/databasedelete */
router.post('/databasedelete',function(req,res){
    console.log(global.token)
    let token = global.token
    axios.post('https://api.weixin.qq.com/tcb/databasedelete?access_token='+token, {
        env: 'test-0pmu0',
        query: 'db.collection(\\"contact\\").where({name:\\"蹇向秋\\"}).remove()'
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/uploadfile */
router.post('/uploadfile',function(req,res){
    console.log(global.token)
    let token = global.token
    axios.post('https://api.weixin.qq.com/tcb/uploadfile?access_token='+token, {
        env: 'test-0pmu0',
        path: 'files/device.json'
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/batchdownloadfile */
router.post('/batchdownloadfile',function(req,res){
    console.log(global.token)
    let token = global.token
    axios.post('https://api.weixin.qq.com/tcb/batchdownloadfile?access_token='+token, {
        env: 'test-0pmu0',
        file_list: [
            {fileid:"cloud://test-0pmu0.7465-test-0pmu0-1300559272/files/device.json",max_age:7200},
            {fileid:"cloud://test-0pmu0.7465-test-0pmu0-1300559272/files/设备.xlsx",max_age:7200}
            ]
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/batchdeletefile */
router.post('/batchdeletefile',function(req,res){
    console.log(global.token)
    let token = global.token
    axios.post('https://api.weixin.qq.com/tcb/batchdeletefile?access_token='+token, {
        env: 'test-0pmu0',
        fileid_list: [
            "cloud://test-0pmu0.7465-test-0pmu0-1300559272/files/device.json",
            "cloud://test-0pmu0.7465-test-0pmu0-1300559272/files/设备.xlsx"
        ]
    })
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})

/* POST http://localhost:8080/api/invokecloudfunction */
router.post('/invokecloudfunction',function(req,res){
    console.log(global.token)
    let token = global.token
    let name = 'http'
    let url='https://api.weixin.qq.com/tcb/invokecloudfunction?access_token='+token+'&env=test-0pmu0&name='+name
    console.log(url)
    axios.post(url)
        .then(function (response) {
            console.log(response.data);
            res.json(response.data)
        })
        .catch(function (error) {
            console.log(error);
        })
})







module.exports = router;
